<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增学习培训')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-study-add">
           <!-- <div class="form-group">
                <label class="col-sm-3 control-label">文件名称：</label>
                <div class="col-sm-8">
                    <input name="fileName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件类型：</label>
                <div class="col-sm-8">
                    <input name="fileType" class="form-control" type="text">
                </div>
            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('study_train_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">上传用户：</label>
                <div class="col-sm-8">
                    <input name="createUser" class="form-control" type="text">
                </div>
            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">有效时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="invalidTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">上传文件：</label>
                <div class="col-sm-8">
                    <input id="filePath" name="filePath"type="file" multiple>
                    <!--<input name="filePath" class="form-control" type="text">-->
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "company/train"
        $("#form-study-add").validate({
            focusCleanup: true
        });
        $(document).ready(function () {
            $("#filePath").fileinput({
                'theme': 'explorer-fas',
                overwriteInitial: false,
                initialPreviewAsData: true,
                language: 'zh', //设置语言
                //uploadUrl: prefix + "/add", //上传的地址
                //allowedFileExtensions: ['jpg', 'gif', 'png','doc','docx','pdf','ppt','pptx','txt'],//接收的文件后缀
                maxFilesNum : 5,//上传最大的文件数量
                //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
                uploadAsync: true, //默认异步上传
                showUpload: false, //是否显示上传按钮
                showRemove : true, //显示移除按钮
                showPreview : true, //是否显示预览
                showCaption: true,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                //dropZoneEnabled: true,//是否显示拖拽区域
                maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
               //minFileCount: 0,
               //maxFileCount: 10, //表示允许同时上传的最大文件个数
                enctype: 'multipart/form-data',
                validateInitialCount:true,
                //previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                //msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
            })

        });

        /*function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-study-add').serialize());
            }
        }*/

        $("input[name='invalidTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

       function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            var formData = new FormData();
            if ($('#filePath')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            for (var i=0;i<$('#filePath')[0].files.length;i++) {
                formData.append('status', $("input[name='status']:checked").val());
                formData.append('invalidTime', $("input[name='invalidTime']").val());
                formData.append('file', $('#filePath')[0].files[i]);
                $.ajax({
                    url: prefix + "/add",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
                formData = new FormData();
            }
           // $.modal.alertWarning("上传成功");
        }
    </script>
</body>
</html>